/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.restorator.service.impl;

import java.util.List;
import java.util.concurrent.Callable;
import org.restorator.dao.base.ItemDao;
import org.restorator.dao.implement.ItemDaoImpl;
import org.restorator.dao.tx.TransactionManager;
import org.restorator.dao.tx.TransactionManagerJdbc;
import org.restorator.dao.tx.TxManagerJdbc;
import org.restorator.entity.Item;
import org.restorator.exception.ServiceException;
import org.restorator.service.ItemService;

/**
 *
 * @author Mama
 */
public class ItemServiceImpl implements ItemService{

    private ItemDao itemDao = new ItemDaoImpl();
    private TransactionManager tx = TxManagerJdbc.INSTANCE;

    public ItemServiceImpl() {
    }
    
    @Override
    public List<Item> getItemsByOrderId(final int id) throws ServiceException {
        return tx.doInTran(new Callable<List<Item>>(){        
            @Override
            public List<Item> call() throws Exception{
                return itemDao.getItemsByOrderId(id);
            }            
        });
    }

    @Override
    public Item get(final Integer id) throws ServiceException {
        return tx.doInTran(new Callable<Item>(){        
            @Override
            public Item call() throws Exception{
                return itemDao.get(id);
            }            
        });    
    }

    @Override
    public void update(final Item id) throws ServiceException {
        tx.doInTran(new Callable<Item>(){        
            @Override
            public Item call() throws Exception{
                return itemDao.update(id);
            }            
        });    
    }

    @Override
    public Integer save(final Item entity, Object... arg) throws ServiceException {
        return tx.doInTran(new Callable<Integer>(){
            @Override
            public Integer call() throws Exception{
                return itemDao.save(entity);
            }
        });
    }

    @Override
    public void delete(final Item entity) throws ServiceException {
        tx.doInTran(new Callable<Item>(){        
            @Override
            public Item call() throws Exception{
                itemDao.delete(entity);
                return entity;
            }            
        });  
    }

    @Override
    public List<Item> getAll() throws ServiceException {
        return tx.doInTran(new Callable<List<Item>>(){        
            @Override
            public List<Item> call() throws Exception{
                return itemDao.getAll();
            }            
        });
    }
    
}
